package util.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import java.util.ArrayList;

import bean.EntityForSimple;
import util.cache.ImageLoaderForReal;


/**
 * 设备Adapter
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class DevicesAdapter extends BaseAdapter {

	private Activity mContext;
	private LayoutInflater mInflater;
	public static ArrayList<EntityForSimple> mDatas;
	private ImageLoaderForReal imageloader;
	public DevicesAdapter(Activity context, ArrayList<EntityForSimple> mDatas) {
		mContext = context;
		this.mDatas = mDatas;
		mInflater = LayoutInflater.from(mContext);
		imageloader= new ImageLoaderForReal(mContext);
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		/*if (convertView == null) {*/
			convertView = mInflater.inflate(R.layout.listview_devices_item,null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
	/*	} else {
			holder = (ViewHolder) convertView.getTag();
		}*/
		if(mDatas.get(position).ischecked()){
			holder.iv_checked.setVisibility(View.VISIBLE);
			DemoApplication.setDeviceid(mDatas.get(position).getDeviceid());
		}else{
			holder.iv_checked.setVisibility(View.GONE);
		}
		holder.tv_state.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				call.listener(position);
				DemoApplication.setDeviceid(mDatas.get(position).getDeviceid());
			}
		});
		holder.tv_name.setText(mDatas.get(position).getDevicename());
		holder.tv_color.setText(mDatas.get(position).getColor());
		holder.tv_state.setText(getStatus(mDatas.get(position).getStatus()));
		holder.tv_address.setText(mDatas.get(position).getPlatenumber());
		holder.tv_shop.setText(mDatas.get(position).getShopname());
		if(!mDatas.get(position).getImg1().isEmpty()){
			imageloader.DisplayImage(mDatas.get(position).getImg1(),holder.iv_logo);
		}else if(!mDatas.get(position).getImg2().isEmpty()){
			imageloader.DisplayImage(mDatas.get(position).getImg2(),holder.iv_logo);
		}else if(!mDatas.get(position).getImg3().isEmpty()){
			imageloader.DisplayImage(mDatas.get(position).getImg3(),holder.iv_logo);
		}
		return convertView;
	}

	private static class ViewHolder {
		/**
		 *  报失4个状态  1.报失审核中 2.报失中  3.已找到  4.已取消
		 *  寻车4个状态  1.未追踪    2.追踪中  3.已找到  4.报失结束
		 */
		public TextView tv_name,tv_color,tv_shop,tv_time,tv_address,tv_state;
        public ImageView iv_logo,iv_checked;
		ViewHolder(View view) {
			tv_name = ((TextView) view.findViewById(R.id.tv_name));
			tv_color = ((TextView) view.findViewById(R.id.tv_color));
			tv_shop = ((TextView) view.findViewById(R.id.tv_shop));
			tv_address = ((TextView) view.findViewById(R.id.tv_address));
			tv_state = ((TextView) view.findViewById(R.id.tv_state));
			iv_logo = ((ImageView) view.findViewById(R.id.iv_logo));
			iv_checked = ((ImageView) view.findViewById(R.id.iv_checked));
		}
	}

	public static interface callback{
		void listener(int item);
	}

	private callback call;

	public void setCallListener(callback call){
		this.call = call;
	}

	private String getStatus(String status){
		String result = "";
		if(status.equals("1")){
			result = "审核中";
		}
		if(status.equals("2")){
			result = "审核失败";
		}
		if(status.equals("3")){
			result = "审核成功";
		}
		return result;
	}
}
